package com.zzu.chat.chat.detail.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.xuexiang.xui.widget.tabbar.EasyIndicator;
import com.zzu.chat.R;
import com.zzu.chat.bean.SubComment;

import com.zzu.chat.bean.Blink;
import com.zzu.chat.bean.Comment;
import com.zzu.chat.util.TimeUtils;

import java.util.LinkedList;
import java.util.List;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.zzu.chat.R.color.grey;

public class Blink_Detail_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LinkedList<Blink> blinks = new LinkedList<>();
    private LinkedList<Comment> comments = new LinkedList<>();
    private Context context ;
    private ViewGroup parent;
    private Comments_Adapter comments_adapter;
    private SubComments_Adapter subComments_adapter;
    LinkedList<SubComment> subComments = new LinkedList<>();

    private final static int TYPE_CONTENT=0;//normal content for blink
    private final static int TYPE_IMAG_HEAD=1;//image in head of body,mean cover,actually,you can use  this for any image
    private final static int TYPE_COMMENTS=3;//comment
    private final static int TYPE_FOOTER=4;//footer


    public Blink_Detail_Adapter(LinkedList<Blink> blinks, Context context){
        this.context = context;
        this.blinks = blinks;

    }

    public Blink_Detail_Adapter(LinkedList<Blink> blinks, LinkedList<Comment> comments, Context context){
        this.context = context;
        this.blinks = blinks;
        this.comments = comments;
    }
    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return TYPE_IMAG_HEAD;
        }
        if (position == 1){
            return TYPE_CONTENT;
        }
        if (position == 2 +comments.size()){
            return TYPE_FOOTER;
        }
        return TYPE_COMMENTS;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_IMAG_HEAD){
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_blink_detail_cover,parent,false);
            return new CoverViewHolder(view);
        }
        if (viewType == TYPE_CONTENT){
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_item_blink_detail_content,parent,false);
            return new ContentViewHolder(view);
        }

        if (viewType == TYPE_COMMENTS){
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_chat_comment_list_item,parent,false);
            return new CommentsViewHolder(view);
        }
        if (viewType == TYPE_FOOTER){
            View view = LayoutInflater.from(context).inflate(R.layout.adapter_commnets_item_footer,parent,false);
            return new FootViewHolder(view);
        }


        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_CONTENT){

            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            if (blinks.size()!=0) {
                contentViewHolder.content.setText(blinks.get(0).getContent());
                contentViewHolder.comments_count.setText("评论数:"+comments.size());
                contentViewHolder.username3.setText(blinks.get(0).getUsername());
            }
        }
        if (getItemViewType(position) == TYPE_IMAG_HEAD){
            CoverViewHolder coverViewHolder = (CoverViewHolder) holder;
        }
        if (getItemViewType(position) == TYPE_COMMENTS){
            CommentsViewHolder commentsViewHolder = (CommentsViewHolder) holder;
            if (comments.size()!=0) {
                commentsViewHolder.comment.setText(comments.get(position - 2).getCommentContent());
                commentsViewHolder.name.setText(AVUser.getCurrentUser().getUsername());
                subComments_adapter = new SubComments_Adapter(subComments,context);
                subComments = commentsViewHolder.get_subcomments(comments.get(position-2).getId());

               /* commentsViewHolder.config_subcomment_adapter(comments.get(position-2).getObjectId());*/
            }


        }
        if (getItemViewType(position) == TYPE_FOOTER){

        }
    }

    @Override
    public int getItemCount() {
        return comments.size()+3;
    }

    //start holder
    class CoverViewHolder extends RecyclerView.ViewHolder {

        private ImageView cover;

        public CoverViewHolder(View ItemView) {
            super(ItemView);
            cover = ItemView.findViewById(R.id.iv_cover);
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {

        private TextView title,content,comments_count,follow, username3;

        public ContentViewHolder(View ItemView) {
            super(ItemView);

            content = ItemView.findViewById(R.id.tv_content);
            title = ItemView.findViewById(R.id.tv_titile);
            comments_count = ItemView.findViewById(R.id.tv_comment_counts_blink_detail);
            follow = ItemView.findViewById(R.id.tv_follow_blink_detail_content);
            username3 = ItemView.findViewById(R.id.read_user_name);
            add_listener_for_follow();
        }
        public void add_listener_for_follow(){
            follow.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onClick(View v) {
                    follow.setText("已关注");
                    follow.setTextColor(grey);
                    Toast.makeText(context,"已关注",Toast.LENGTH_SHORT);
                }
            });
        }
    }

    class CommentsViewHolder extends RecyclerView.ViewHolder {
        private List<View> viewList = new LinkedList<>();
        private View pager1,pager2;//1 for comments,2 for repost etc
        private ImageView headImage,iv_subcomment;
        private TextView comment,time,name,tv_subcomment;
        private EasyIndicator mEasyIndicator;
        private String[] tabTitles;
        private ViewPager viewPager;

        private Comments_PagerAdapter comments_pagerAdapter;
        private RecyclerView recyclerView_pager1,recyclerView_subcomments;

        public CommentsViewHolder(View ItemView) {
            super(ItemView);
            name = ItemView.findViewById(R.id.tv_user_name);
            comment = ItemView.findViewById(R.id.comment_content);
            iv_subcomment = ItemView.findViewById(R.id.iv_comment_list_item);
            tv_subcomment = ItemView.findViewById(R.id.tv_content_subcomment_item);
            recyclerView_subcomments = ItemView.findViewById(R.id.rv_subcomments);

            config_subcomment_adapter();
        }
        public void add_listener_for_subcomment(){

        }
        public void config_subcomment_adapter(String id){
            subComments_adapter = new SubComments_Adapter(get_subcomments(id),context);
            recyclerView_subcomments.setLayoutManager(new LinearLayoutManager(context));
            recyclerView_subcomments.setAdapter(subComments_adapter);
        }
        public void config_subcomment_adapter(){
            subComments_adapter = new SubComments_Adapter(subComments,context);
            recyclerView_subcomments.setLayoutManager(new LinearLayoutManager(context));
            recyclerView_subcomments.setAdapter(subComments_adapter);
        }
        public LinkedList<SubComment> get_subcomments(String id){
            /*
            * here get by commentid
            * */
            AVQuery<AVObject> query = new AVQuery<>("SubComment");
            /*query.whereEqualTo("Blink",blink);*/
            query.orderByDescending("createdAt");
            query.whereEqualTo("parent_comment_id",id);
            query.findInBackground().subscribe(new Observer<List<AVObject>>() {
                public void onSubscribe(Disposable disposable) {}
                public void onNext(List<AVObject> students) {
                    for (AVObject avObject : students) {
                        subComments.add(TimeUtils.getSubCommentClass(avObject));
                    }
                    subComments_adapter.notifyDataSetChanged();
                    Log.d("检查读取到的评论数量",String.valueOf(subComments.size()));
                }
                public void onError(Throwable throwable) {}
                public void onComplete() {}
            });
            return subComments;
        }


        /**
         * configure tab
         * */
        private void configure_tab(){
            tabTitles = new String[]{"评论","转发"};
            mEasyIndicator.setTabTitles(tabTitles);
            viewList.add(pager1);
            viewList.add(pager2);
            comments_pagerAdapter = new Comments_PagerAdapter(viewList);
            mEasyIndicator.setViewPager(viewPager, comments_pagerAdapter);
        }
        /**show comments in pager1,configure recyclerview data,and use adapter
        *{@link Comments_Adapter}for it.
        */
        public void configure_recyclerview_in_pager1(){
            recyclerView_pager1.setLayoutManager(new LinearLayoutManager(context));
            recyclerView_pager1.setAdapter(comments_adapter);
        }
    }

    private ContentLoadingProgressBar progressBar;
     class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
            progressBar=itemView.findViewById(R.id.pb_progress);
        }
    }

    //end holder

    //start api
    public void set_progressbar_unvisible(){
        if (progressBar!=null)
        progressBar.setVisibility(View.INVISIBLE);
    }

    /**
    *expose CommentAdapter ,for update its data
     */
    @Deprecated
    public Comments_Adapter get_CommentsAdapter(){
        return comments_adapter;
    }


    @Deprecated
    public void set_CommentAdapter(Comments_Adapter comments_adapter){
        this.comments_adapter = comments_adapter;
    }
    //end api

}
